package com.tjbank.cssys.web.controller;

import com.github.pagehelper.PageInfo;
import com.tjbank.cssys.domain.CkLog;
import com.tjbank.cssys.domain.CkStopNoticePerson;
import com.tjbank.cssys.repository.CkStopNoticePersonMapper;
import com.tjbank.cssys.web.service.CkLogService;
import com.tjbank.cssys.web.service.CkOutsourcingCompanyService;
import com.tjbank.cssys.web.service.CkStopNoticePersonService;
import com.tjbank.cssys.web.util.CommonResult;
import com.tjbank.cssys.web.util.GetUserCodeFromToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@Api(tags = "CkStopNoticePersonController", description = "失联/停催人员名单管理", basePath = "/stop/notice")
@RequestMapping("/stop/notice")
@RestController
public class CkStopNoticePersonController {

    private static final Byte TC_TYPE = 1;//停催
    private static final Byte SL_TYPE = 2;//失联

    @Autowired
    private CkStopNoticePersonService ckStopNoticePersonService;
    @Autowired
    private CkOutsourcingCompanyService ckOutsourcingCompanyService;
    @Autowired
    private CkStopNoticePersonMapper ckStopNoticePersonMapper;
    @Autowired
    private CkLogService ckLogService;

   private final static Logger logger = LoggerFactory.getLogger(CkStopNoticePersonController.class);

    @ApiOperation("查询 失联/停催人员名单 数据列表")
    @PostMapping("/list")
    public CommonResult<PageInfo> selectList(@RequestBody Map<String, Object> map, HttpServletRequest request) {
        PageInfo<CkStopNoticePerson> pageData = ckStopNoticePersonService.selectList(map);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("查询失联/停催人员名单数据列表");

        if (pageData != null) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(pageData);
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("错误");
        }
    }

    @ApiOperation("失联/停催人员名单 人工添加")
    @PostMapping("/add")
    public CommonResult addStopNoticePerson(@RequestBody CkStopNoticePerson params, HttpServletRequest request) {
        int resultCount = ckStopNoticePersonMapper.getPeopleByCardNumber(params);
        if (resultCount > 0 && params.getType()==1) {
            return CommonResult.success(null, "停催已经有此人");
        }
        if (resultCount > 0 && params.getType()==2) {
            return CommonResult.success(null, "失联已经有此人");
        }
        boolean result = ckStopNoticePersonService.insert(params);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams(params + "");
        ckLog.setOperationType("失联/停催人员名单人工添加");

        if (result) {
            ckLog.setResult("success:200");
            ckLogService.addInsert(ckLog);
            return CommonResult.success(null, "添加成功！");
        } else {
            ckLog.setResult("error:500");
            ckLogService.addInsert(ckLog);
            return CommonResult.failed("添加失败！");
        }
    }

    @ApiOperation("失联/停催人员名单 批量删除")
    @GetMapping("/delete/batch")
    public CommonResult deletePerson(@RequestParam String Ids, HttpServletRequest request) {
        if (StringUtils.isNotBlank(Ids)) {
            //ckStopNoticePersonService.deleteById(id);
            ckStopNoticePersonService.deleteByIds(Ids);
        }
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams(Ids + "");
        ckLog.setOperationType("失联/停催人员名单批量删除");
        ckLog.setResult("success:200");
        return CommonResult.success(null, "删除成功！");
    }


    @ApiOperation("更新保存接口")
    @PostMapping("/update/info")
    public CommonResult updateInfo(@RequestBody CkStopNoticePerson params, HttpServletRequest request) {
        ArrayList<CkStopNoticePerson> list = new ArrayList<>();
        list.add(params);
        ckStopNoticePersonService.updateForeach(list);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams("");
        ckLog.setOperationType("更新保存接口停催人员名单");
        ckLog.setResult("success:200");
        return CommonResult.success(null, "保存成功！");
    }

    @ApiOperation("失联/停催人员名单 出库（物理删除）")
    @GetMapping("/delete/{id}")
    public CommonResult deletePerson(@PathVariable Long id, HttpServletRequest request) {
        if (null != id) {
            ckStopNoticePersonService.deleteById(id);
        }
        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());

        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams(id + "");
        ckLog.setOperationType("失联/停催人员名单 出库（物理删除）");
        ckLog.setResult("success:200");
        return CommonResult.success(null, "删除成功！");
    }

    @ApiOperation("将参数ids 中数据的状态设置为失联")
    @PostMapping("/update/type")
    public CommonResult updateType(@RequestParam String ids, HttpServletRequest request) {

        ckStopNoticePersonService.updateType(ids, SL_TYPE);

        String userCode = null;
        try {
            userCode = GetUserCodeFromToken.getUserCode(request);
        } catch (Exception e) {
            logger.error("获取登录人信息异常"+e.getMessage());
        }
        CkLog ckLog = new CkLog();
        ckLog.setLogId(UUID.randomUUID().toString());
        ckLog.setUserId(userCode);
        Date date = new Date();
        ckLog.setTimes(new Date(date.getTime()));
        ckLog.setLogName(UUID.randomUUID().toString());
        ckLog.setParams(ids + "");
        ckLog.setOperationType("将参数ids 中数据的状态设置为失联");
        ckLog.setResult("success:200");
        return CommonResult.success(null, "操作成功！");
    }

}
